package com.qms.qep.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.core.page.TableDataInfo;
import com.qms.common.enums.BusinessType;
import com.qms.common.utils.poi.ExcelUtil;
import com.qms.qep.domain.Batch;
import com.qms.qep.domain.ProcessRecord;
import com.qms.qep.domain.vo.BatchTraceVO;
import com.qms.qep.domain.vo.ProductionStatistics;
import com.qms.qep.service.IBatchService;
import com.qms.qep.service.IProcessRecordService;

/**
 * 生产管理Controller
 * 
 * @author qep
 */
@RestController
@RequestMapping("/qep/production")
public class ProductionController extends BaseController
{
    @Autowired
    private IBatchService batchService;
    
    @Autowired
    private IProcessRecordService processRecordService;

    /**
     * 查询批次列表
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:list')")
    @GetMapping("/batch/list")
    public TableDataInfo listBatches(Batch batch)
    {
        startPage();
        List<Batch> list = batchService.selectBatchList(batch);
        return getDataTable(list);
    }
    
    /**
     * 导出批次报表
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:export')")
    @Log(title = "批次信息", businessType = BusinessType.EXPORT)
    @GetMapping("/batch/export")
    public AjaxResult exportBatch(Batch batch)
    {
        List<Batch> list = batchService.selectBatchList(batch);
        ExcelUtil<Batch> util = new ExcelUtil<Batch>(Batch.class);
        return util.exportExcel(list, "批次数据");
    }

    /**
     * 获取批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:query')")
    @GetMapping(value = "/batch/{id}")
    public AjaxResult getBatch(@PathVariable("id") Long id)
    {
        return success(batchService.selectBatchById(id));
    }
    
    /**
     * 批次追溯
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:trace')")
    @GetMapping(value = "/batch/trace/{batchCode}")
    public AjaxResult traceBatch(@PathVariable("batchCode") String batchCode)
    {
        BatchTraceVO traceInfo = batchService.traceBatch(batchCode);
        if (traceInfo == null) {
            return error("未找到批次信息");
        }
        return success(traceInfo);
    }

    /**
     * 新增批次
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:add')")
    @Log(title = "批次信息", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody Batch batch)
    {
        return toAjax(batchService.insertBatch(batch));
    }

    /**
     * 修改批次信息
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:edit')")
    @Log(title = "批次信息", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    public AjaxResult editBatch(@RequestBody Batch batch)
    {
        return toAjax(batchService.updateBatch(batch));
    }

    /**
     * 删除批次
     */
    @PreAuthorize("@ss.hasPermi('qep:production:batch:remove')")
    @Log(title = "批次信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/batch/{ids}")
    public AjaxResult removeBatch(@PathVariable Long[] ids)
    {
        return toAjax(batchService.deleteBatchByIds(ids));
    }
    
    /**
     * 查询工序流转记录列表
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:flow:list')")
    @GetMapping("/process/flow/list")
    public TableDataInfo listProcessFlows(ProcessRecord processRecord)
    {
        startPage();
        List<ProcessRecord> list = processRecordService.selectProcessRecordList(processRecord);
        return getDataTable(list);
    }

    /**
     * 获取工序流转记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:flow:query')")
    @GetMapping(value = "/process/flow/{id}")
    public AjaxResult getProcessFlow(@PathVariable("id") Long id)
    {
        return success(processRecordService.selectProcessRecordById(id));
    }

    /**
     * 新增工序流转记录
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:flow:add')")
    @Log(title = "工序流转记录", businessType = BusinessType.INSERT)
    @PostMapping("/process/flow")
    public AjaxResult addProcessFlow(@RequestBody ProcessRecord processRecord)
    {
        return toAjax(processRecordService.insertProcessRecord(processRecord));
    }

    /**
     * 修改工序流转记录
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:flow:edit')")
    @Log(title = "工序流转记录", businessType = BusinessType.UPDATE)
    @PutMapping("/process/flow")
    public AjaxResult editProcessFlow(@RequestBody ProcessRecord processRecord)
    {
        return toAjax(processRecordService.updateProcessRecord(processRecord));
    }

    /**
     * 删除工序流转记录
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:flow:remove')")
    @Log(title = "工序流转记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/process/flow/{ids}")
    public AjaxResult removeProcessFlow(@PathVariable Long[] ids)
    {
        return toAjax(processRecordService.deleteProcessRecordByIds(ids));
    }
    
    /**
     * 获取工序效率统计
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:efficiency')")
    @GetMapping("/process/efficiency")
    public AjaxResult getProcessEfficiency(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long processTypeId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("processTypeId", processTypeId);
        return success(processRecordService.selectProcessEfficiency(params));
    }
    
    /**
     * 获取瓶颈工序分析
     */
    @PreAuthorize("@ss.hasPermi('qep:production:process:bottleneck')")
    @GetMapping("/process/bottleneck")
    public AjaxResult getBottleneckAnalysis(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        return success(processRecordService.selectBottleneckAnalysis(params));
    }
    
    /**
     * 获取生产周期分析
     */
    @PreAuthorize("@ss.hasPermi('qep:production:cycle:analysis')")
    @GetMapping("/cycle/analysis")
    public AjaxResult getProductionCycleAnalysis(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) Long categoryId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("productId", productId);
        params.put("categoryId", categoryId);
        return success(processRecordService.selectProductionCycleAnalysis(params));
    }
    
    /**
     * 获取生产进度数据
     */
    @PreAuthorize("@ss.hasPermi('qep:production:progress')")
    @GetMapping("/progress")
    public AjaxResult getProductionProgress(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long productId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("productId", productId);
        return success(processRecordService.selectProductionProgress(params));
    }
    
    /**
     * 获取生产统计数据
     */
    @PreAuthorize("@ss.hasPermi('qep:production:statistics')")
    @GetMapping("/statistics")
    public AjaxResult getProductionStatistics(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long processTypeId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("productId", productId);
        params.put("categoryId", categoryId);
        params.put("processTypeId", processTypeId);
        
        ProductionStatistics statistics = processRecordService.getProductionStatistics(params);
        return success(statistics);
    }
} 